﻿#light

module Dwarf_StructuredGrid_Generator

open System
open System.IO
open System.Text
open System.ComponentModel

let Dwarf_StructuredGrid_Generator (fs : FileStream, args : String[]) = 

    // subsidiary objects
    let writer = new StreamWriter(fs);
    let rand = new Random();

    // constants
    let epsilon = 2;
    let average_byte_count = 4 + 1  // 4 bytes for digits, 1 byte for ' ' between two values in row
    let count_of_digits_in_kilobyte = 1024
    let max_int = 10000

    // formulaes
    let division x = (float) (x * count_of_digits_in_kilobyte / average_byte_count)
    let dimension x = (int) (Math.Round(Math.Sqrt(division x)))

    // current data values
    let count_of_kilobytes = (int) args.[args.Length - 1]
    let current_dimension = dimension count_of_kilobytes

    // write the dimension
    writer.Write(epsilon);
    writer.WriteLine();
    writer.Write(current_dimension);
    writer.WriteLine();
    writer.Write(current_dimension);
    writer.WriteLine();

    for i = 0 to current_dimension - 1 do
        for j = 0 to current_dimension - 1 do 

            // write the next random value
            writer.Write(rand.Next(max_int));
            writer.Write(' ');
        done
        writer.WriteLine();
    done

    writer.Close()

    true